package com.excesys.exsecs.energy.service.impl;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.excesys.exsecs.energy.entity.*;
import com.excesys.exsecs.energy.mapper.DevAreaMapMapper;
import com.excesys.exsecs.energy.service.CostHourFlowService;
import com.excesys.exsecs.energy.service.DevAreaMapService;
import com.excesys.exsecs.energy.service.PowerHourFlowService;
import com.excesys.exsecs.energy.service.WaterHourFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域维度表和设备映射表
 *
 * @author lxc
 * @date 2024-02-26 14:12:29
 */
@Service
public class DevAreaMapServiceImpl extends ServiceImpl<DevAreaMapMapper, DevAreaMap> implements DevAreaMapService {

    //小时 - 电
    @Autowired
    private PowerHourFlowService powerHourFlowService;

    //小时 - 水
    @Autowired
    private WaterHourFlowService waterHourFlowService;

    //能耗费用小时统计表
    @Autowired
    private CostHourFlowService costHourFlowService;

    //电
    @Override
    public JSONObject powerAreaData(String id, String startDate, String endDate, String compareType) throws ParseException {
        //总能耗量 上期能耗量 环比增长率
        JSONObject jsonPower = new JSONObject();
        //日期时间字符串转换
        DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //获取数据库最新的时间格式 开始时间 与 结束时间
        Map newestTime = getNewestTime(startDate, endDate);
        //当日时间与当月时间
        String startTime = null;//起始时间
        String endTime = null;//结束时间
        //环比的时间
        LocalDateTime startDateTime = null;
        LocalDateTime endDateTime = null;
        if (!newestTime.isEmpty()) {
            startDateTime = LocalDateTime.parse(newestTime.get("startTime").toString(), isoFormatter);
            startTime = startDateTime.format(customFormatter);
            endDateTime = LocalDateTime.parse(newestTime.get("endTime").toString(), isoFormatter);
            endTime = endDateTime.format(customFormatter);
        }

        //环比时间
        String YoYStartTime = null;
        String YoYendTime = null;
        //日
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            //获取左天的时间用来做判断用
            LocalDate today = LocalDate.now();
            // 判断开始时间和结束时间是否都在今天
            if (!startDate.equals(today.toString()) && !endDate.equals(today.toString())) {
                if (startDate.equals(endDate)) {
                    LocalDate days = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(days.toString(), days.toString());
                    if (!newest.isEmpty()) {
                        LocalDateTime nextDayDateTime = startDateTime.plusDays(1);
                        // 格式化并打印明天的日期时间
                        LocalDateTime dateTime1 = LocalDateTime.parse(nextDayDateTime.toString(), isoFormatter);
                        endTime = dateTime1.format(customFormatter);
                    }
                    //2 代表 环比逻辑处理 - 上期能耗
                    if (compareType.equals("2")) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusDays(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = startTime;
                    }
                } else {
                    //区间查询，根据结束时间，获取明天的00:00:00点
                    endTime = LocalDateTime.parse(endTime, customFormatter).toLocalDate().plusDays(1).atStartOfDay().format(customFormatter);
                }
            } else {
                //2 代表 环比逻辑处理 - 上期能耗
                if (compareType.equals("2") || compareType.equals("0")) {
                    if (startDateTime != null && endDateTime != null) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusDays(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = LocalDateTime.parse(endDateTime.minusDays(1).toString(), isoFormatter).format(customFormatter);
                    }
                }
            }
        }
        //月
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            //1.获取当前月
            YearMonth currentMonth = YearMonth.from(LocalDate.now());
            if (!startDate.equals(currentMonth.toString()) && !endDate.equals(currentMonth.toString())) {
                if (startDate.equals(endDate)) {
                    YearMonth month = YearMonth.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM")).plusMonths(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(month.toString(), month.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                    //2 代表 环比逻辑处理 - 上期能耗
                    if (compareType.equals("2")) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusMonths(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = startTime;
                    }
                } else {
                    //区间查询，根据结束时间，获取明天的00:00:00点
                    YearMonth month = YearMonth.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM")).plusMonths(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(month.toString(), month.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                }
            } else {
                //2 代表 环比逻辑处理 - 上期能耗
                if (compareType.equals("2") || compareType.equals("0")) {
                    YoYStartTime = LocalDateTime.parse(startDateTime.minusMonths(1).toString(), isoFormatter).format(customFormatter);
                    YoYendTime = LocalDateTime.parse(endDateTime.minusMonths(1).toString(), isoFormatter).format(customFormatter);
                }
            }
        }
        //年
        if (startDate.matches("^\\d{4}$") && endDate.matches("^\\d{4}$")) {
            //1.获取当前年
            Year month = Year.from(LocalDate.now());
            if (!startDate.equals(month.toString()) && !endDate.equals(month.toString())) {
                if (startDate.equals(endDate)) {
                    Year year = Year.parse(startDate, DateTimeFormatter.ofPattern("yyyy")).plusYears(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(year.toString(), year.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                    //2 代表 环比逻辑处理 - 上期能耗
                    if (compareType.equals("1")) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusYears(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = startTime;
                    }
                } else {
                    //区间查询，根据结束时间，获取明年的00:00:00点
                    Year year = Year.parse(endDate, DateTimeFormatter.ofPattern("yyyy")).plusYears(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(year.toString(), year.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                }
            } else {
                //2 代表 环比逻辑处理 - 上期能耗
                if (compareType.equals("1") || compareType.equals("0")) {
                    YoYStartTime = LocalDateTime.parse(startDateTime.minusYears(1).toString(), isoFormatter).format(customFormatter);
                    YoYendTime = LocalDateTime.parse(endDateTime.minusYears(1).toString(), isoFormatter).format(customFormatter);
                }
            }
        }
        double sumPower = 0;
        if (startTime != null && endTime != null) {
            //能耗统计 - 反减
            double startSumPower = this.baseMapper.getSumTotalPower(startTime, startTime.substring(0, 7).replace("-", ""), id);
            double endSumPower = this.baseMapper.getSumTotalPower(endTime, endTime.substring(0, 7).replace("-", ""), id);
            if (startSumPower != 0 && endSumPower != 0) {
                sumPower = endSumPower - startSumPower;
            }

        }
        //总能耗量
        jsonPower.put("sumPower", new BigDecimal(sumPower).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        double YoYsumPower = 0;
        if (YoYStartTime != null && YoYendTime != null) {
            double YoYstartSumPower = this.baseMapper.getSumTotalPower(YoYStartTime, YoYStartTime.substring(0, 7).replace("-", ""), id);
            double YoYendSumPower = this.baseMapper.getSumTotalPower(YoYendTime, YoYendTime.substring(0, 7).replace("-", ""), id);
            if (YoYstartSumPower != 0 && YoYendSumPower != 0) {
                YoYsumPower = YoYendSumPower - YoYstartSumPower;
            }
        }

        //上期能耗量
        jsonPower.put("contrastPower", new BigDecimal(YoYsumPower).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        // 环比增长率
        if (YoYsumPower > 0) {
            Double growthRate = (sumPower - YoYsumPower) / YoYsumPower * 100; // 增长率
            BigDecimal b = new BigDecimal(growthRate);
            double growthRatef = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            jsonPower.put("growthRate", growthRatef);
        } else {
            jsonPower.put("growthRate", "-");
        }
        return jsonPower;
    }

    //水
    @Override
    public JSONObject powerAreaWaterData(String id, String startDate, String endDate, String compareType) throws ParseException {
        //总能耗量 上期能耗量 环比增长率
        JSONObject jsonPower = new JSONObject();
        //日期时间字符串转换
        DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //获取数据库最新的时间格式 开始时间 与 结束时间
        Map newestTime = getNewestWaterTime(startDate, endDate);
        //当日时间与当月时间
        String startTime = null;//起始时间
        String endTime = null;//结束时间
        //环比的时间
        LocalDateTime startDateTime = null;
        LocalDateTime endDateTime = null;
        if (!newestTime.isEmpty()) {
            startDateTime = LocalDateTime.parse(newestTime.get("startTime").toString(), isoFormatter);
            startTime = startDateTime.format(customFormatter);
            endDateTime = LocalDateTime.parse(newestTime.get("endTime").toString(), isoFormatter);
            endTime = endDateTime.format(customFormatter);
        }

        //环比时间
        String YoYStartTime = null;
        String YoYendTime = null;
        //日
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            //获取左天的时间用来做判断用
            LocalDate today = LocalDate.now();
            // 判断开始时间和结束时间是否都在今天
            if (!startDate.equals(today.toString()) && !endDate.equals(today.toString())) {
                if (startDate.equals(endDate)) {
                    LocalDate days = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(days.toString(), days.toString());
                    if (!newest.isEmpty()) {
                        LocalDateTime nextDayDateTime = startDateTime.plusDays(1);
                        // 格式化并打印明天的日期时间
                        LocalDateTime dateTime1 = LocalDateTime.parse(nextDayDateTime.toString(), isoFormatter);
                        endTime = dateTime1.format(customFormatter);
                    }
                    //2 代表 环比逻辑处理 - 上期能耗
                    if (compareType.equals("2")) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusDays(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = startTime;
                    }
                } else {
                    //区间查询，根据结束时间，获取明天的00:00:00点
                    endTime = LocalDateTime.parse(endTime, customFormatter).toLocalDate().plusDays(1).atStartOfDay().format(customFormatter);
                }
            } else {
                //2 代表 环比逻辑处理 - 上期能耗
                if (compareType.equals("2") || compareType.equals("0")) {
                    if (startDateTime != null && endDateTime != null) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusDays(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = LocalDateTime.parse(endDateTime.minusDays(1).toString(), isoFormatter).format(customFormatter);
                    }
                }
            }
        }
        //月
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            //1.获取当前月
            YearMonth currentMonth = YearMonth.from(LocalDate.now());
            if (!startDate.equals(currentMonth.toString()) && !endDate.equals(currentMonth.toString())) {
                if (startDate.equals(endDate)) {
                    YearMonth month = YearMonth.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM")).plusMonths(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(month.toString(), month.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                    //2 代表 环比逻辑处理 - 上期能耗
                    if (compareType.equals("2")) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusMonths(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = startTime;
                    }
                } else {
                    //区间查询，根据结束时间，获取明天的00:00:00点
                    YearMonth month = YearMonth.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM")).plusMonths(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(month.toString(), month.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                }
            } else {
                //2 代表 环比逻辑处理 - 上期能耗
                if (compareType.equals("2") || compareType.equals("0")) {
                    YoYStartTime = LocalDateTime.parse(startDateTime.minusMonths(1).toString(), isoFormatter).format(customFormatter);
                    YoYendTime = LocalDateTime.parse(endDateTime.minusMonths(1).toString(), isoFormatter).format(customFormatter);
                }
            }
        }
        //年
        if (startDate.matches("^\\d{4}$") && endDate.matches("^\\d{4}$")) {
            //1.获取当前年
            Year month = Year.from(LocalDate.now());
            if (!startDate.equals(month.toString()) && !endDate.equals(month.toString())) {
                if (startDate.equals(endDate)) {
                    Year year = Year.parse(startDate, DateTimeFormatter.ofPattern("yyyy")).plusYears(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(year.toString(), year.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                    //2 代表 环比逻辑处理 - 上期能耗
                    if (compareType.equals("1")) {
                        YoYStartTime = LocalDateTime.parse(startDateTime.minusYears(1).toString(), isoFormatter).format(customFormatter);
                        YoYendTime = startTime;
                    }
                } else {
                    //区间查询，根据结束时间，获取明年的00:00:00点
                    Year year = Year.parse(endDate, DateTimeFormatter.ofPattern("yyyy")).plusYears(1);
                    //获取开始时间与结束时间 用来做判断用 明天有没有数据, 没有数据则不进入
                    Map newest = getNewestTime(year.toString(), year.toString());
                    if (!newest.isEmpty()) {
                        // 格式化并打印明天的日期时间
                        endTime = LocalDateTime.parse(newest.get("startTime").toString(), isoFormatter).format(customFormatter);
                    }
                }
            } else {
                //2 代表 环比逻辑处理 - 上期能耗
                if (compareType.equals("1") || compareType.equals("0")) {
                    YoYStartTime = LocalDateTime.parse(startDateTime.minusYears(1).toString(), isoFormatter).format(customFormatter);
                    YoYendTime = LocalDateTime.parse(endDateTime.minusYears(1).toString(), isoFormatter).format(customFormatter);
                }
            }
        }
        double sumPower = 0;
        if (startTime != null && endTime != null) {
            //能耗统计 - 反减
            double startSumPower = this.baseMapper.getSumTotalWater("2", startTime, null, id);
            double endSumPower = this.baseMapper.getSumTotalWater("2", endTime, null, id);
            if (startSumPower != 0 && endSumPower != 0) {
                sumPower = endSumPower - startSumPower;
            }
        }
        //总能耗量
        jsonPower.put("sumPower", new BigDecimal(sumPower).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        double YoYsumPower = 0;
        if (YoYStartTime != null && YoYendTime != null) {
            double YoYstartSumPower = this.baseMapper.getSumTotalWater("2", YoYStartTime, null, id);
            double YoYendSumPower = this.baseMapper.getSumTotalWater("2", YoYendTime, null, id);
            if (YoYstartSumPower != 0 && YoYendSumPower != 0) {
                YoYsumPower = YoYendSumPower - YoYstartSumPower;
            }
        }
        //上期能耗量
        jsonPower.put("contrastPower", new BigDecimal(YoYsumPower).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        // 环比增长率
        if (YoYsumPower > 0) {
            Double growthRate = (sumPower - YoYsumPower) / YoYsumPower * 100; // 增长率
            BigDecimal b = new BigDecimal(growthRate);
            double growthRatef = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            jsonPower.put("growthRate", growthRatef);
        } else {
            jsonPower.put("growthRate", "-");
        }
        return jsonPower;
    }

    //电
    @Override
    public JSONObject powerAreaLayer(String id, String startDate, String endDate, String compareType) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        //日
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            LocalDate today = LocalDate.now();
            //当天数据
            if (startDate.equals(today.toString()) && endDate.equals(today.toString())) {
                //日能耗柱状图
                jsonPower = powerDayLayer(startDate, endDate, compareType, id);
            } else {
                if (startDate.equals(endDate)) {
                    //其他天的数据
                    jsonPower = powerDayLayer(startDate, endDate, compareType, id);
                } else {
                    jsonPower = powerDaySectionLayer(startDate, endDate, compareType, id);
                }
            }
        }
        //月
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            //1.获取当前月
            YearMonth currentMonth = YearMonth.from(LocalDate.now());
            //前月数据
            if (startDate.equals(currentMonth.toString()) && endDate.equals(currentMonth.toString())) {
                jsonPower = powerMonthLayer(startDate, endDate, compareType, id);
            } else {
                if (startDate.equals(endDate)) {
                    jsonPower = powerMonthLayer(startDate, endDate, compareType, id);
                } else {
                    jsonPower = powerMonthSectionLayer(startDate, endDate, compareType, id);
                }
            }
        }
        // 年
        if (startDate.matches("^\\d{4}$") && endDate.matches("^\\d{4}$")) {
            //1.获取当前年
            Year month = Year.from(LocalDate.now());
            if (startDate.equals(month.toString()) && endDate.equals(month.toString())) {
                jsonPower = powerYearsLayer(startDate, endDate, compareType, id);
            } else {
                if (startDate.equals(endDate)) {
                    jsonPower = powerYearsLayer(startDate, endDate, compareType, id);
                } else {
                    jsonPower = powerYearsSectionLayer(startDate, endDate, compareType, id);
                }
            }
        }
        return jsonPower;
    }

    //日能耗柱状图
    public JSONObject powerDayLayer(String startDate, String endDate, String compareType, String id) {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<Double> yData2 = new ArrayList<>();//其他
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        String otherStartTime = null;
        String otherEndTime = null;
        List<Map<String, Object>> otherChartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerList = new ArrayList<>();
        // Create图层时间
        layerStartTime = startDate + " 01";//开始时间
        // 解析给定的日期字符串
        LocalDate givenDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 获取明天的日期
        LocalDate nextDay = givenDate.plusDays(1);
        layerEndTime = nextDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00";
        if (compareType.equals("2")) {
            // Create图层其他时间
            // 获取昨天的日期
            LocalDate previousDay = givenDate.minusDays(1);
            otherStartTime = previousDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 01";
            otherEndTime = endDate + " 00";
            //其他时间段
            otherChartPowerList = this.baseMapper.chartPower(otherStartTime, otherEndTime, id);
        }
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartPower(layerStartTime, layerEndTime, id);
        for (int h = 0; h < 24; h++) {
            String strHour = String.format("%02d", h);
            xData.add(strHour);
            List<Map<String, Object>> yearData = chartPowerList.stream().filter(bo -> bo.get("createTime").equals(strHour))
                    .collect(Collectors.toList());
            Double powerData = null;
            Double otherData = null;
            if (!yearData.isEmpty()) {
                powerData = Double.parseDouble(yearData.get(0).get("totalValue").toString());
                yData1.add(new BigDecimal(powerData).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData1.add(0.0);
            }
            List<Map<String, Object>> otherYearData = new ArrayList<>();
            if (!otherChartPowerList.isEmpty()) {
                otherYearData = otherChartPowerList.stream().filter(bo -> bo.get("createTime").equals(strHour))
                        .collect(Collectors.toList());
            }
            if (!otherYearData.isEmpty()) {
                otherData = Double.parseDouble(otherYearData.get(0).get("totalValue").toString());
                yData2.add(new BigDecimal(otherData).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData2.add(0.0);
            }
        }


        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        chartData.put("yData2", yData2);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerList);//表格
        return jsonPower;
    }

    //日 - 区间 - 柱状图
    public JSONObject powerDaySectionLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        //模糊查询的字段
        List<Map<String, Object>> chartPowerList = new ArrayList<>();//最新数据
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        // 使用输入格式解析日期字符串
        SimpleDateFormat inputSdf = new SimpleDateFormat("yyyyMMdd");
        // 使用输出格式格式化日期对象
        SimpleDateFormat outputSdf = new SimpleDateFormat("yyyy-MM-dd");

        layerStartTime = startDate.replaceAll("-", "");
        layerEndTime = endDate.replaceAll("-", "");
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartMonthPower(null, layerStartTime, layerEndTime, id);
        if (!chartPowerList.isEmpty()) {
            for (Map<String, Object> mapList : chartPowerList) {
                xData.add(outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
                yData1.add(new BigDecimal(Double.parseDouble(mapList.get("totalValue").toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                Map<String, Object> otherChartMap = new HashMap<>();
                otherChartMap.put("totalValue", mapList.get("totalValue").toString());
                otherChartMap.put("createTime", mapList.get("createTime").toString());
                otherChartMap.put("timeValue", outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
                chartPowerListtable.add(otherChartMap);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerListtable);//表格
        return jsonPower;
    }

    //月能耗柱状图
    public JSONObject powerMonthLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<Double> yData2 = new ArrayList<>();//其他
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        String otherStartTime = null;
        String otherEndTime = null;

        List<Map<String, Object>> otherChartPowerList = new ArrayList<>();//其他时间段
        List<Map<String, Object>> chartPowerList = new ArrayList<>();//最新数据
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        //模糊查询的字段
        String date = startDate.replaceAll("-", "");
        if (compareType.equals("2")) {
            //开始时间与结束时间是否相同
            // 创建一个YearMonth对象
            YearMonth yearMonth = YearMonth.of(Integer.parseInt(startDate.substring(0, 4)), Integer.parseInt(startDate.substring(6, 7)));
            // 获取上一个月的YearMonth
            YearMonth previousYearMonth = yearMonth.minusMonths(1);
            String BlurDate = previousYearMonth.toString().replaceAll("-", "");
            //环比 柱状图 其他时间段
            otherChartPowerList = this.baseMapper.chartMonthPower(BlurDate, otherStartTime, otherEndTime, id);
        }
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartMonthPower(date, layerStartTime, layerEndTime, id);
        for (int day = 1; day <= 31; day++) {
            // 输出两位数的日期，如果是一位数则在前面补0
            String strDay = String.format("%02d", day);
            xData.add(strDay);
            List<Map<String, Object>> dayData = chartPowerList.stream().filter(bo -> bo.get("createTime").equals(strDay))
                    .collect(Collectors.toList());
            Double powerDayData = null;
            Double otherChart = null;
            if (!dayData.isEmpty()) {
                powerDayData = Double.parseDouble(dayData.get(0).get("totalValue").toString());
                yData1.add(new BigDecimal(powerDayData).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData1.add(0.0);
            }
            List<Map<String, Object>> otherChartData = new ArrayList<>();
            if (!otherChartPowerList.isEmpty()) {
                otherChartData = otherChartPowerList.stream().filter(bo -> bo.get("createTime").equals(strDay))
                        .collect(Collectors.toList());
            }
            if (!otherChartData.isEmpty()) {
                otherChart = Double.parseDouble(otherChartData.get(0).get("totalValue").toString());
                yData2.add(new BigDecimal(otherChart).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData2.add(0.0);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        chartData.put("yData2", yData2);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerList);//表格
        return jsonPower;
    }

    //月 - 区间 - 柱状图
    public JSONObject powerMonthSectionLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<String> tableData = new ArrayList<>();

        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        List<Map<String, Object>> chartPowerList = new ArrayList<>();//最新数据
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        // 使用输入格式解析日期字符串
        SimpleDateFormat inputSdf = new SimpleDateFormat("yyyyMM");
        // 使用输出格式格式化日期对象
        SimpleDateFormat outputSdf = new SimpleDateFormat("yyyy-MM");
        layerStartTime = startDate.replaceAll("-", "");
        layerEndTime = endDate.replaceAll("-", "");
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartYearPower(null, layerStartTime, layerEndTime, id);
        for (Map<String, Object> mapList : chartPowerList) {
            xData.add(outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
            yData1.add(new BigDecimal(Double.parseDouble(mapList.get("totalValue").toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            Map<String, Object> otherChartMap = new HashMap<>();
            otherChartMap.put("totalValue", mapList.get("totalValue").toString());
            otherChartMap.put("createTime", mapList.get("createTime").toString());
            otherChartMap.put("timeValue", outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
            chartPowerListtable.add(otherChartMap);
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerListtable);//表格
        jsonPower.put("table", tableData);
        return jsonPower;
    }

    //年能耗柱状图
    public JSONObject powerYearsLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<Double> yData2 = new ArrayList<>();//其他
        //模糊查询的字段
        String date = null;
        //图层时间
        String layerStartTime = null;
        String otherStartTime = null;
        List<Map<String, Object>> otherChartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();
        layerStartTime = startDate;
        //上年
        if (compareType.equals("1")) {
            otherStartTime = String.valueOf(Integer.parseInt(startDate) - 1);
            otherChartPowerList = this.baseMapper.chartYearPower(otherStartTime, "", null, id);
        }
        /*图表*/
        //最新数据
        chartPowerList = this.baseMapper.chartYearPower(layerStartTime, "", "", id);
        for (int m = 1; m < 13; m++) {
            String strMonth = String.format("%02d", m);
            List<Map<String, Object>> chartYearData = chartPowerList.stream().filter(bo -> bo.get("createTime").equals(strMonth))
                    .collect(Collectors.toList());
            Double YearData = null;
            Double chartYear = null;
            if (!chartYearData.isEmpty()) {
                YearData = Double.parseDouble(chartYearData.get(0).get("totalValue").toString());
                yData1.add(YearData);
            } else {
                yData1.add(0.0);
            }
            List<Map<String, Object>> otherChartYearData = new ArrayList<>();
            if (!otherChartPowerList.isEmpty()) {
                otherChartYearData = otherChartPowerList.stream().filter(bo -> bo.get("createTime").equals(strMonth))
                        .collect(Collectors.toList());
            }
            if (!otherChartYearData.isEmpty()) {
                chartYear = Double.parseDouble(otherChartYearData.get(0).get("totalValue").toString());
                yData2.add(chartYear);
            } else {
                yData2.add(0.0);
            }
            xData.add(strMonth);
        }

        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        chartData.put("yData2", yData2);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerList);//表格
        return jsonPower;
    }

    //年 - 区间 - 柱状图
    public JSONObject powerYearsSectionLayer(String startDate, String endDate, String compareType, String id) {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        List<Map<String, Object>> chartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        if (compareType.equals("0")) {
            //年的区间 开始时间
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(startDate), Calendar.JANUARY, 1); // 年份、月份（从0开始，1代表1月）、日期
            layerStartTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(calendar.getTime());
            //年的区间 接受时间
            calendar.set(Integer.parseInt(endDate), Calendar.DECEMBER, 31); // 年份、月份（从0开始，1代表1月）、日期
            layerEndTime = new SimpleDateFormat("yyyy-MM-dd 23:00:00").format(calendar.getTime());
            /*图表*/
            //最新数据
            chartPowerList = this.baseMapper.chartMonthSection(layerStartTime, layerEndTime, id);
            for (Map<String, Object> mapList : chartPowerList) {
                xData.add(mapList.get("timeYear").toString());
                yData1.add(new BigDecimal(Double.parseDouble(mapList.get("totalValue").toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                Map<String, Object> otherChartMap = new HashMap<>();
                otherChartMap.put("totalValue", mapList.get("totalValue").toString());
                otherChartMap.put("createTime", mapList.get("timeYear").toString());
                otherChartMap.put("timeValue", mapList.get("timeYear").toString());
                chartPowerListtable.add(otherChartMap);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerListtable);//表格
        return jsonPower;
    }

    //水
    @Override
    public JSONObject powerAreaWaterLayer(String id, String startDate, String endDate, String compareType) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        //日
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            LocalDate today = LocalDate.now();
            //当天数据
            if (startDate.equals(today.toString()) && endDate.equals(today.toString())) {
                //日能耗柱状图
                jsonPower = powerDayWaterLayer(startDate, endDate, compareType, id);
            } else {
                if (startDate.equals(endDate)) {
                    //其他天的数据
                    jsonPower = powerDayWaterLayer(startDate, endDate, compareType, id);
                } else {
                    jsonPower = powerDaySectionWaterLayer(startDate, endDate, compareType, id);
                }
            }
        }
        //月
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            //1.获取当前月
            YearMonth currentMonth = YearMonth.from(LocalDate.now());
            //前月数据
            if (startDate.equals(currentMonth.toString()) && endDate.equals(currentMonth.toString())) {
                jsonPower = powerMonthWaterLayer(startDate, endDate, compareType, id);
            } else {
                if (startDate.equals(endDate)) {
                    jsonPower = powerMonthWaterLayer(startDate, endDate, compareType, id);
                } else {
                    jsonPower = powerMonthSectionWaterLayer(startDate, endDate, compareType, id);
                }
            }
        }
        // 年
        if (startDate.matches("^\\d{4}$") && endDate.matches("^\\d{4}$")) {
            //1.获取当前年
            Year month = Year.from(LocalDate.now());
            if (startDate.equals(month.toString()) && endDate.equals(month.toString())) {
                jsonPower = powerYearsWaterLayer(startDate, endDate, compareType, id);
            } else {
                if (startDate.equals(endDate)) {
                    jsonPower = powerYearsWaterLayer(startDate, endDate, compareType, id);
                } else {
                    jsonPower = powerYearsSectionWaterLayer(startDate, endDate, compareType, id);
                }
            }
        }
        return jsonPower;
    }

    //日能耗柱状图
    public JSONObject powerDayWaterLayer(String startDate, String endDate, String compareType, String id) {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<Double> yData2 = new ArrayList<>();//其他
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        String otherStartTime = null;
        String otherEndTime = null;
        List<Map<String, Object>> otherChartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerList = new ArrayList<>();
        // Create图层时间
        layerStartTime = startDate + " 01";//开始时间
        // 解析给定的日期字符串
        LocalDate givenDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 获取明天的日期
        LocalDate nextDay = givenDate.plusDays(1);
        layerEndTime = nextDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00";
        if (compareType.equals("2")) {
            // Create图层其他时间
            // 获取昨天的日期
            LocalDate previousDay = givenDate.minusDays(1);
            otherStartTime = previousDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 01";
            otherEndTime = endDate + " 00";
            //其他时间段
            otherChartPowerList = this.baseMapper.chartWater(otherStartTime, otherEndTime, id);
        }
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartWater(layerStartTime, layerEndTime, id);
        for (int h = 0; h < 24; h++) {
            String strHour = String.format("%02d", h);
            xData.add(strHour);
            List<Map<String, Object>> yearData = chartPowerList.stream().filter(bo -> bo.get("createTime").equals(strHour))
                    .collect(Collectors.toList());
            Double powerData = null;
            Double otherData = null;
            if (!yearData.isEmpty()) {
                powerData = Double.parseDouble(yearData.get(0).get("totalValue").toString());
                yData1.add(new BigDecimal(powerData).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData1.add(0.0);
            }
            List<Map<String, Object>> otherYearData = new ArrayList<>();
            if (!otherChartPowerList.isEmpty()) {
                otherYearData = otherChartPowerList.stream().filter(bo -> bo.get("createTime").equals(strHour))
                        .collect(Collectors.toList());
            }
            if (!otherYearData.isEmpty()) {
                otherData = Double.parseDouble(otherYearData.get(0).get("totalValue").toString());
                yData2.add(new BigDecimal(otherData).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData2.add(0.0);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        chartData.put("yData2", yData2);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerList);//表格
        return jsonPower;
    }

    //日 - 区间 - 柱状图
    public JSONObject powerDaySectionWaterLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        //模糊查询的字段
        List<Map<String, Object>> chartPowerList = new ArrayList<>();//最新数据
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        // 使用输入格式解析日期字符串
        SimpleDateFormat inputSdf = new SimpleDateFormat("yyyyMMdd");
        // 使用输出格式格式化日期对象
        SimpleDateFormat outputSdf = new SimpleDateFormat("yyyy-MM-dd");
        layerStartTime = startDate.replaceAll("-", "");
        layerEndTime = endDate.replaceAll("-", "");
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartMonthWater(null, layerStartTime, layerEndTime, id);
        if (!chartPowerList.isEmpty()) {
            for (Map<String, Object> mapList : chartPowerList) {
                xData.add(outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
                yData1.add(new BigDecimal(Double.parseDouble(mapList.get("totalValue").toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                Map<String, Object> otherChartMap = new HashMap<>();
                otherChartMap.put("totalValue", mapList.get("totalValue").toString());
                otherChartMap.put("createTime", mapList.get("createTime").toString());
                otherChartMap.put("timeValue", outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
                chartPowerListtable.add(otherChartMap);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerListtable);//表格
        return jsonPower;
    }

    //月能耗柱状图
    public JSONObject powerMonthWaterLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<Double> yData2 = new ArrayList<>();//其他
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        String otherStartTime = null;
        String otherEndTime = null;
        List<Map<String, Object>> otherChartPowerList = new ArrayList<>();//其他时间段
        List<Map<String, Object>> chartPowerList = new ArrayList<>();//最新数据
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        //模糊查询的字段
        String date = startDate.replaceAll("-", "");
        if (compareType.equals("2")) {
            //开始时间与结束时间是否相同
            // 创建一个YearMonth对象
            YearMonth yearMonth = YearMonth.of(Integer.parseInt(startDate.substring(0, 4)), Integer.parseInt(startDate.substring(6, 7)));
            // 获取上一个月的YearMonth
            YearMonth previousYearMonth = yearMonth.minusMonths(1);
            String BlurDate = previousYearMonth.toString().replaceAll("-", "");
            //环比 柱状图 其他时间段
            otherChartPowerList = this.baseMapper.chartMonthWater(BlurDate, otherStartTime, otherEndTime, id);
        }

        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartMonthWater(date, layerStartTime, layerEndTime, id);
        for (int day = 1; day <= 31; day++) {
            // 输出两位数的日期，如果是一位数则在前面补0
            String strDay = String.format("%02d", day);
            xData.add(strDay);
            List<Map<String, Object>> dayData = chartPowerList.stream().filter(bo -> bo.get("createTime").equals(strDay))
                    .collect(Collectors.toList());
            Double powerDayData = null;
            Double otherChart = null;
            if (!dayData.isEmpty()) {
                powerDayData = Double.parseDouble(dayData.get(0).get("totalValue").toString());
                yData1.add(new BigDecimal(powerDayData).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData1.add(0.0);
            }
            List<Map<String, Object>> otherChartData = new ArrayList<>();
            if (!otherChartPowerList.isEmpty()) {
                otherChartData = otherChartPowerList.stream().filter(bo -> bo.get("createTime").equals(strDay))
                        .collect(Collectors.toList());
            }
            if (!otherChartData.isEmpty()) {
                otherChart = Double.parseDouble(otherChartData.get(0).get("totalValue").toString());
                yData2.add(new BigDecimal(otherChart).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                yData2.add(0.0);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        chartData.put("yData2", yData2);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerList);//表格
        return jsonPower;
    }

    //月 - 区间 - 柱状图
    public JSONObject powerMonthSectionWaterLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<String> tableData = new ArrayList<>();

        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        List<Map<String, Object>> chartPowerList = new ArrayList<>();//最新数据
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        // 使用输入格式解析日期字符串
        SimpleDateFormat inputSdf = new SimpleDateFormat("yyyyMM");
        // 使用输出格式格式化日期对象
        SimpleDateFormat outputSdf = new SimpleDateFormat("yyyy-MM");
        layerStartTime = startDate.replaceAll("-", "");
        layerEndTime = endDate.replaceAll("-", "");
        /* 图表 */
        //最新数据
        chartPowerList = this.baseMapper.chartYearWater(null, layerStartTime, layerEndTime, id);
        for (Map<String, Object> mapList : chartPowerList) {
            xData.add(outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
            yData1.add(new BigDecimal(Double.parseDouble(mapList.get("totalValue").toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            Map<String, Object> otherChartMap = new HashMap<>();
            otherChartMap.put("totalValue", mapList.get("totalValue").toString());
            otherChartMap.put("createTime", mapList.get("createTime").toString());
            otherChartMap.put("timeValue", outputSdf.format(inputSdf.parse(mapList.get("timeValue").toString())));
            chartPowerListtable.add(otherChartMap);
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerListtable);//表格
        jsonPower.put("table", tableData);
        return jsonPower;
    }

    //年能耗柱状图
    public JSONObject powerYearsWaterLayer(String startDate, String endDate, String compareType, String id) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        List<Double> yData2 = new ArrayList<>();//其他
        //模糊查询的字段
        String date = null;
        //图层时间
        String layerStartTime = null;
        String otherStartTime = null;
        List<Map<String, Object>> otherChartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();
        layerStartTime = startDate;
        //上年
        if (compareType.equals("1")) {
            otherStartTime = String.valueOf(Integer.parseInt(startDate) - 1);
            otherChartPowerList = this.baseMapper.chartYearWater(otherStartTime, "", null, id);
        }
        /*图表*/
        //最新数据
        chartPowerList = this.baseMapper.chartYearWater(layerStartTime, "", "", id);
        for (int m = 1; m < 13; m++) {
            String strMonth = String.format("%02d", m);
            List<Map<String, Object>> chartYearData = chartPowerList.stream().filter(bo -> bo.get("createTime").equals(strMonth))
                    .collect(Collectors.toList());
            Double YearData = null;
            Double chartYear = null;
            if (!chartYearData.isEmpty()) {
                YearData = Double.parseDouble(chartYearData.get(0).get("totalValue").toString());
                yData1.add(YearData);
            } else {
                yData1.add(0.0);
            }
            List<Map<String, Object>> otherChartYearData = new ArrayList<>();
            if (!otherChartPowerList.isEmpty()) {
                otherChartYearData = otherChartPowerList.stream().filter(bo -> bo.get("createTime").equals(strMonth))
                        .collect(Collectors.toList());
            }
            if (!otherChartYearData.isEmpty()) {
                chartYear = Double.parseDouble(otherChartYearData.get(0).get("totalValue").toString());
                yData2.add(chartYear);
            } else {
                yData2.add(0.0);
            }
            xData.add(strMonth);
        }

        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        chartData.put("yData2", yData2);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerList);//表格
        return jsonPower;
    }

    //年 - 区间 - 柱状图
    public JSONObject powerYearsSectionWaterLayer(String startDate, String endDate, String compareType, String id) {
        JSONObject jsonPower = new JSONObject();
        /*图表通用数组*/
        JSONObject chartData = new JSONObject();
        List<String> xData = new ArrayList<>();
        List<Double> yData1 = new ArrayList<>();//当前
        //图层时间
        String layerStartTime = null;
        String layerEndTime = null;
        List<Map<String, Object>> chartPowerList = new ArrayList<>();
        List<Map<String, Object>> chartPowerListtable = new ArrayList<>();//区间表格数据接受
        if (compareType.equals("0")) {
            //年的区间 开始时间
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(startDate), Calendar.JANUARY, 1); // 年份、月份（从0开始，1代表1月）、日期
            layerStartTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(calendar.getTime());
            //年的区间 接受时间
            calendar.set(Integer.parseInt(endDate), Calendar.DECEMBER, 31); // 年份、月份（从0开始，1代表1月）、日期
            layerEndTime = new SimpleDateFormat("yyyy-MM-dd 23:00:00").format(calendar.getTime());
            /*图表*/
            //最新数据
            chartPowerList = this.baseMapper.chartMonthWaterSection(layerStartTime, layerEndTime, id);
            for (Map<String, Object> mapList : chartPowerList) {
                xData.add(mapList.get("timeYear").toString());
                yData1.add(new BigDecimal(Double.parseDouble(mapList.get("totalValue").toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                Map<String, Object> otherChartMap = new HashMap<>();
                otherChartMap.put("totalValue", mapList.get("totalValue").toString());
                otherChartMap.put("createTime", mapList.get("timeYear").toString());
                otherChartMap.put("timeValue", mapList.get("timeYear").toString());
                chartPowerListtable.add(otherChartMap);
            }
        }
        chartData.put("xData", xData);
        chartData.put("yData1", yData1);
        jsonPower.put("chartData", chartData);//图标
        jsonPower.put("tableData", chartPowerListtable);//表格
        return jsonPower;
    }


    //桑基图
    @Override
    public List<Map<String, Object>> powerAreaLinks(String startTime, String endTime, List<String> idsList) {
        return this.baseMapper.powerAreaLinks(startTime, endTime, idsList);
    }

    @Override
    public List<Map<String, Object>> dayReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.dayReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> daySectionReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.daySectionReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> monthReport(String date, List<String> idList) {
        return this.baseMapper.monthReport(date, idList);
    }

    @Override
    public List<Map<String, Object>> monthSectionReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.monthSectionReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> yearReport(String date, List<String> idList) {
        return this.baseMapper.yearReport(date, idList);
    }

    @Override
    public List<Map<String, Object>> yearSectionReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.yearSectionReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> dayWaterReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.dayWaterReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> dayWaterSectionReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.dayWaterSectionReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> monthWaterReport(String date, List<String> idList) {
        return this.baseMapper.monthWaterReport(date, idList);
    }

    @Override
    public List<Map<String, Object>> monthWaterSectionReport(String startTime, String endTime, List<String> idList) {
        return this.baseMapper.monthWaterSectionReport(startTime, endTime, idList);
    }

    @Override
    public List<Map<String, Object>> yearWaterReport(String date, List<String> idList) {
        return this.baseMapper.yearWaterReport(date, idList);
    }

    @Override
    public List<Map<String, Object>> areaSortRank(String startTime, String endTime, List<String> idList, String energyType) {
        return this.baseMapper.areaSortRank(startTime, endTime, idList, energyType);
    }

    @Override
    public List<Map<String, Object>> areaSortWaterRank(String startTime, String endTime, List<String> idList, String energyType) {
        return this.baseMapper.areaSortWaterRank(startTime, endTime, idList, energyType);
    }

    @Override
    public JSONArray powerCostData(String id, String startDate, String endDate) throws ParseException {
        String layerStartTime = null;
        String layerEndTime = null;
        List<Map<String, Object>> reportlist = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        //时
        if (startDate.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}$") && endDate.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}$")) {
            reportlist = costHourFlowService.areaCostData(startDate, endDate, id);
        }
        //日
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            //当天数据
            if (startDate.equals(endDate)) {
                //   Create图层时间
                layerStartTime = startDate + " 01";//开始时间
                // 解析给定的日期字符串
                LocalDate givenDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                // 获取明天的日期
                LocalDate nextDay = givenDate.plusDays(1);
                layerEndTime = nextDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
            } else {
                //   Create图层时间
                layerStartTime = startDate + " 01";//开始时间
                // 解析给定的日期字符串
                LocalDate givenDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                // 获取明天的日期
                LocalDate nextDay = givenDate.plusDays(1);
                layerEndTime = nextDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
            }
        }
        //月
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            //前月数据
            if (startDate.equals(endDate)) {
                // 解析字符串为日期对象
                calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(startDate));
                // 获取这个月的第一天
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                Date firstDayOfMonth = calendar.getTime();
                layerStartTime = sdf.format(firstDayOfMonth).toString() + " 01";//开始时间
                // 获取下个月的第一天
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                Date firstDayOfNextMonth = calendar.getTime();
                layerEndTime = sdf.format(firstDayOfNextMonth) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
            } else {
                // 处理 startDate
                calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(startDate));
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                layerStartTime = sdf.format(calendar.getTime()).toString() + " 01";//开始时间
                // 处理 endDate
                calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(endDate));
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                layerEndTime = sdf.format(calendar.getTime()) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
            }
        }
        // 年
        if (startDate.matches("^\\d{4}$") && endDate.matches("^\\d{4}$")) {
            // 处理 startDate，获取年的第一天
            calendar.set(Integer.parseInt(startDate), 0, 1);
            layerStartTime = sdf.format(calendar.getTime()).toString() + " 01";//开始时间
            // 处理 endDate，获取年下一年的第一天
            calendar.set(Integer.parseInt(endDate) + 1, 0, 1);
            layerEndTime = sdf.format(calendar.getTime()) + " 00";
            reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
        }
        return optimizeReportList(reportlist);
    }

    public static JSONArray optimizeReportList(List<Map<String, Object>> reportlist) {
        JSONArray resultArray = new JSONArray();
        double totalTotalValue = 0;
        double totalSumPrice = 0;
        for (int rateType = 1; rateType <= 4; rateType++) {
            String finalRateType = String.valueOf(rateType);
            List<Map<String, Object>> filteredList = reportlist.stream()
                    .filter(bo -> bo.get("rateType").equals(finalRateType))
                    .collect(Collectors.toList());
            JSONObject rateTypeObject;
            if (!filteredList.isEmpty()) {
                Map<String, Object> firstItem = filteredList.get(0);
                rateTypeObject = new JSONObject();
                rateTypeObject.put("totalValue", new BigDecimal(String.valueOf(firstItem.get("totalValue"))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                rateTypeObject.put("rateType", firstItem.get("rateType"));
                rateTypeObject.put("sumPrice", new BigDecimal(String.valueOf(firstItem.get("sumPrice"))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                rateTypeObject.put("price", firstItem.get("price"));
                // 累加 totalValue 和 sumPrice
                totalTotalValue += (double) firstItem.get("totalValue");
                totalSumPrice += (double) firstItem.get("sumPrice");
            } else {
                rateTypeObject = new JSONObject();
                rateTypeObject.put("totalValue", 0);
                rateTypeObject.put("rateType", finalRateType);
                rateTypeObject.put("sumPrice", 0);
                rateTypeObject.put("price", 0);
            }
            resultArray.put(rateTypeObject);
        }

        // 创建 totals 对象并添加到结果数组中
        JSONObject totalsObject = new JSONObject();
        totalsObject.put("sumPrice", new BigDecimal(totalSumPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        BigDecimal averagePrice;
        if (totalTotalValue == 0) {
            averagePrice = BigDecimal.ZERO;
        } else {
            averagePrice = new BigDecimal(totalSumPrice).divide(new BigDecimal(totalTotalValue), 2, BigDecimal.ROUND_HALF_UP);
        }
        totalsObject.put("price", averagePrice.doubleValue());
        totalsObject.put("totalValue", new BigDecimal(totalTotalValue).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        totalsObject.put("rateType", "0");
        resultArray.put(totalsObject);
        return resultArray;
    }

    @Override
    public JSONObject powerCostBingtu(String id, String startDate, String endDate) throws ParseException {
        JSONObject jsonPower = new JSONObject();
        String layerStartTime = null;
        String layerEndTime = null;
        List<Map<String, Object>> reportlist = new ArrayList<>();
        List<Map<String, Object>> listBingtu = new ArrayList<>();
        String type = "0"; //1日 2月 3年 4其他
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        //时
        if (startDate.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}$") && endDate.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}$")) {
            reportlist = costHourFlowService.areaCostData(startDate, endDate, id);
            listBingtu = costHourFlowService.areaCostDataBingtu(startDate, endDate, id);
            type = "4";
        }
        //日
        if (startDate.matches("\\d{4}-\\d{2}-\\d{2}") && endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            //当天数据
            if (startDate.equals(endDate)) {
                //   Create图层时间
                layerStartTime = startDate + " 01";//开始时间
                // 解析给定的日期字符串
                LocalDate givenDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                // 获取明天的日期
                LocalDate nextDay = givenDate.plusDays(1);
                layerEndTime = nextDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00";
                System.out.println(layerStartTime);
                System.out.println(layerEndTime);
                System.out.println(id);
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
                listBingtu = costHourFlowService.areaCostDayBingtu(layerStartTime, layerEndTime, id);
                type = "1";
            } else {
                //   Create图层时间
                layerStartTime = startDate + " 01";//开始时间
                // 解析给定的日期字符串
                LocalDate givenDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                // 获取明天的日期
                LocalDate nextDay = givenDate.plusDays(1);
                layerEndTime = nextDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
                listBingtu = costHourFlowService.areaCostDayBingtuSection(startDate, endDate, id);
                type = "4";
            }
        }
        //月
        if (startDate.matches("^\\d{4}-(0[1-9]|1[0-2])$") && endDate.matches("^\\d{4}-(0[1-9]|1[0-2])$")) {
            //前月数据
            if (startDate.equals(endDate)) {
                // 解析字符串为日期对象
                calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(startDate));
                // 获取这个月的第一天
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                Date firstDayOfMonth = calendar.getTime();
                layerStartTime = sdf.format(firstDayOfMonth).toString() + " 01";//开始时间
                // 获取下个月的第一天
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                Date firstDayOfNextMonth = calendar.getTime();
                layerEndTime = sdf.format(firstDayOfNextMonth) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
                listBingtu = costHourFlowService.areaCostMonthBingtu(layerStartTime, layerEndTime, id);
                type = "2";
            } else {
                // 处理 startDate
                calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(startDate));
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                layerStartTime = sdf.format(calendar.getTime()).toString() + " 01";//开始时间
                // 处理 endDate
                calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(endDate));
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                layerEndTime = sdf.format(calendar.getTime()) + " 00";
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
                listBingtu = costHourFlowService.areaCostMonthBingtuSection(layerStartTime, layerEndTime, id);
                type = "4";
            }
        }
        // 年
        if (startDate.matches("^\\d{4}$") && endDate.matches("^\\d{4}$")) {
            // 处理 startDate，获取年的第一天
            calendar.set(Integer.parseInt(startDate), 0, 1);
            layerStartTime = sdf.format(calendar.getTime()).toString() + " 01";//开始时间
            // 处理 endDate，获取年下一年的第一天
            calendar.set(Integer.parseInt(endDate) + 1, 0, 1);
            layerEndTime = sdf.format(calendar.getTime()) + " 00";
            if (startDate.equals(endDate)) {
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
                listBingtu = costHourFlowService.areaCostYearsBingtu(layerStartTime, layerEndTime, id);
                type = "3";
            } else {
                reportlist = costHourFlowService.areaCostData(layerStartTime, layerEndTime, id);
                listBingtu = costHourFlowService.areaCostYearsBingtuSection(layerStartTime, layerEndTime, id);
                type = "4";
            }
        }
        JSONArray resultArray = new JSONArray();
        //饼图数据展示
        for (int rateType = 1; rateType <= 4; rateType++) {
            String finalRateType = String.valueOf(rateType);
            List<Map<String, Object>> filteredList = reportlist.stream()
                    .filter(bo -> bo.get("rateType").equals(finalRateType))
                    .collect(Collectors.toList());
            JSONObject rateTypeObject;
            if (!filteredList.isEmpty()) {
                Map<String, Object> firstItem = filteredList.get(0);
                rateTypeObject = new JSONObject();
                rateTypeObject.put("value", new BigDecimal(String.valueOf(firstItem.get("sumPrice"))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                rateTypeObject.put("name", getRateTypeName(Integer.parseInt(finalRateType)));
            } else {
                rateTypeObject = new JSONObject();
                rateTypeObject.put("value", 0);
                rateTypeObject.put("name", getRateTypeName(Integer.parseInt(finalRateType)));
            }
            resultArray.put(rateTypeObject);
        }
        jsonPower.put("data", resultArray);
        JSONObject ratelistBingtu = getRatelistBingtu(type, listBingtu);
        //堆叠柱状图
        jsonPower.put("stacked", ratelistBingtu);
        return jsonPower;
    }

    private static String getRateTypeName(int rateType) {
        switch (rateType) {
            case 1:
                return "尖电";
            case 2:
                return "峰电";
            case 3:
                return "平电";
            case 4:
                return "谷电";
            default:
                return null;
        }
    }

    private JSONObject getRatelistBingtu(String type, List<Map<String, Object>> listBingtu) {
        JSONObject result = new JSONObject();
        List<String> xAxis = new ArrayList<>();
        //日
        if (type.equals("1")) {
            for (int h = 1; h < 25; h++) {
                String strHour = String.format("%02d", h);
                xAxis.add(strHour);
            }
        }
        //月
        if (type.equals("2")) {
            for (int day = 1; day <= 31; day++) {
                // 输出两位数的日期，如果是一位数则在前面补0
                String strDay = String.format("%02d", day);
                xAxis.add(strDay);
            }
        }
        //年
        if (type.equals("3")) {
            for (int m = 1; m < 13; m++) {
                String strMonth = String.format("%02d", m);
                xAxis.add(strMonth);
            }
        }
        //其他
        if (type.equals("4")) {
            List<String> finalXAxis = xAxis;
            listBingtu.forEach(r -> {
                finalXAxis.add(r.get("createTime").toString());
            });
            xAxis = finalXAxis.stream()
                    .distinct() // 去重
                    .sorted() // 排序（自然顺序）
                    .collect(Collectors.toList());
        }
        List<Map<String, Object>> formattedSeries = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            Map<String, Object> seriesMap = new HashMap<>();
            seriesMap.put("name", getRateTypeName(i));
            List<Double> dataList = new ArrayList<>();
            int finalI = i;
            xAxis.forEach(x -> {
                List<Map<String, Object>> data = listBingtu.stream()
                        .filter(y -> y.get("createTime").toString().equals(x.toString()) && Integer.parseInt(y.get("rateType").toString()) == finalI)
                        .collect(Collectors.toList());
                Double powerData = 0.0;
                if (!data.isEmpty()) {
                    powerData = Double.parseDouble(data.get(0).get("sumPrice").toString());
                }
                dataList.add(new BigDecimal(String.valueOf(powerData)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            });
            seriesMap.put("data", dataList);
            formattedSeries.add(seriesMap);
        }

        result.put("series", formattedSeries);
        result.put("xAxisData", xAxis);
        return result;
    }


    //获取数据库最新的时间格式 开始时间 与 结束时间
    //电
    public Map getNewestTime(String startDate, String endDate) {
        Map map = new HashMap();
        //获取当前时间
        PowerHourFlow startHourFlow = this.powerHourFlowService.getBaseMapper().selectOne(Wrappers.<PowerHourFlow>lambdaQuery()
                .like(PowerHourFlow::getTimeValue, startDate)
                .orderByAsc(PowerHourFlow::getTimeValue).last("LIMIT 1"));
        //获取上一条数据
        PowerHourFlow endHourFlow = this.powerHourFlowService.getBaseMapper().selectOne(Wrappers.<PowerHourFlow>lambdaQuery()
                .like(PowerHourFlow::getTimeValue, endDate)
                .orderByDesc(PowerHourFlow::getTimeValue).last("LIMIT 1"));
        if (ObjectUtils.isNotEmpty(startHourFlow) && ObjectUtils.isNotEmpty(endHourFlow)) {
            map.put("startTime", startHourFlow.getTimeValue().toString());
            map.put("endTime", endHourFlow.getTimeValue().toString());
        }
        return map;
    }

    //水
    public Map getNewestWaterTime(String startDate, String endDate) {
        Map map = new HashMap();
        //获取当前时间
        WaterHourFlow startHourFlow = this.waterHourFlowService.getBaseMapper().selectOne(Wrappers.<WaterHourFlow>lambdaQuery()
                .like(WaterHourFlow::getTimeValue, startDate)
                .orderByAsc(WaterHourFlow::getTimeValue).last("LIMIT 1"));
        //获取上一条数据
        WaterHourFlow endHourFlow = this.waterHourFlowService.getBaseMapper().selectOne(Wrappers.<WaterHourFlow>lambdaQuery()
                .like(WaterHourFlow::getTimeValue, endDate)
                .orderByDesc(WaterHourFlow::getTimeValue).last("LIMIT 1"));
        if (ObjectUtils.isNotEmpty(startHourFlow) && ObjectUtils.isNotEmpty(endHourFlow)) {
            map.put("startTime", startHourFlow.getTimeValue().toString());
            map.put("endTime", endHourFlow.getTimeValue().toString());
        }
        return map;
    }
}
